package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.adapter.TagConsts;
import sk.naive.talker.props.PropertyStoreException;
import sk.naive.talker.message.DefaultMessageFactory;
import sk.naive.talker.util.Utils;
import sk.naive.talker.persistence.PersistenceException;

import java.util.*;
import java.rmi.RemoteException;
import java.text.DecimalFormat;

/**
 * Examine displays various user's properties.
 *
 * @author <a href="mailto:richter@bgs.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.44 $ $Date: 2005/02/16 19:27:21 $
 */
public class Examine extends AbstractCommand {

	public void exec() throws CommandException, RemoteException, PropertyStoreException {
		try {
			User examined = user;
			if (params != null) {
				examined = commandDispatcher.getUserFinder().findUserByLogin(params);
				if (examined == null) {
					sendUserNotFoundMessage(params);
					return;
				}
			}

			StringBuilder sb = new StringBuilder();
			sb.append(getString("examine.header", examined.getProperties(), user.getLang()));

			Iterator i = commandDispatcher.textPersistence().loadTexts(examined.getId(), TextRelation.USER_PROFILE, "").iterator();
			if (i.hasNext()) {
				sb.append(((Text) i.next()).getContent());
				sb.append(Utils.tag(TagConsts.BR));
			} else {
				sb.append(getString("examine.noProfile", examined.getProperties(), user.getLang()));
			}
			UserTimeStatisticsCalculator calculator = new UserTimeStatisticsCalculator(examined);
			if (examined.isOnline()) {
				if (calculator.isAfk() && calculator.afkReason() != null) {
					ctxSet(examined, DefaultMessageFactory.CTXKEY_VAL + "AfkReason", calculator.afkReason());
				}
				ctxSet(examined, DefaultMessageFactory.CTXKEY_VAL + "idle", Utils.createLong(calculator.idleTime(), Consts.MINUTE_MILLIS));
				ctxSet(examined, DefaultMessageFactory.CTXKEY_VAL + "session", Utils.createLong(calculator.sessionTime(), Consts.MINUTE_MILLIS));
				alwaysShown(calculator);
				sb.append(getString("examine.online", examined.getProperties(), user.getLang()));
			} else {
				ctxSet(examined, DefaultMessageFactory.CTXKEY_VAL + "session", Utils.createLong(examined.get(User.UPROP_LAST_SESSION_TIME), Consts.MINUTE_MILLIS));
				alwaysShown(calculator);
				ctxSet(examined, DefaultMessageFactory.CTXKEY_VAL + "last", Utils.createDate(examined.get(User.UPROP_LAST_LOGIN)));
				sb.append(getString("examine.offline", examined.getProperties(), user.getLang()));
			}
			user.send(sb.toString());
		} catch (PersistenceException e) {
			throw new CommandException(e);
		}
	}

	private void alwaysShown(UserTimeStatisticsCalculator calculator) throws PropertyStoreException {
		ctxSet(calculator.getUser(), DefaultMessageFactory.CTXKEY_VAL + "totalLoginTime", Utils.createLong(calculator.getTotalLoginTime(), Consts.MINUTE_MILLIS));
		ctxSet(calculator.getUser(), DefaultMessageFactory.CTXKEY_VAL + "afkPercent", percent(calculator.getTotalLoginTime(), calculator.getTotalAfkTime()));
		ctxSet(calculator.getUser(), DefaultMessageFactory.CTXKEY_VAL + "idlePercent", percent(calculator.getTotalLoginTime(), calculator.getTotalIdleTime()));
		ctxSet(calculator.getUser(), DefaultMessageFactory.CTXKEY_VAL + "created", Utils.createDate(calculator.getUser().get(User.UPROP_CREATED)));
	}

	private String percent(long base, long part) {
		DecimalFormat f = new DecimalFormat("0.00");
		return f.format(100d * part / base);
	}
}
